home *** CD-ROM | disk | FTP | other *** search
- Attribute VB_Name = "uucode"
- Option Explicit
- '---------------------------------------------------
- 'UUCODE.BAS
- 'Copyright 1996 by Carl Franklin
- 'Unauthorized reproduction in any medium of this
- 'source code is strictly prohibited without written
- 'permission from the author and John Wiley & Sons.
- '---------------------------------------------------
-
- Function Decode(szData As String) As String
-
- On Error GoTo HepMe
-
- Dim szOut As String
- Dim nChar As Integer
- Dim I As Integer
-
- For I = 1 To Len(szData) Step 4
- szOut = szOut & Chr((Asc(Mid(szData, I, 1)) - 32) * 4 + (Asc(Mid(szData, I + 1, 1)) - 32) \ 16)
- szOut = szOut & Chr((Asc(Mid(szData, I + 1, 1)) Mod 16) * 16 + (Asc(Mid(szData, I + 2, 1)) - 32) \ 4)
- szOut = szOut & Chr((Asc(Mid(szData, I + 2, 1)) Mod 4) * 64 + Asc(Mid(szData, I + 3, 1)) - 32)
- Next I
-
- Decode = szOut
-
- Exit Function
-
- HepMe:
-
- Stop
-
- End Function
-
- Function Encode(szData As String) As String
-
- Dim szOut As String
- Dim nChar As Integer
- Dim I As Integer
-
- ' pad to 3 byte multiple
- If Len(szData) Mod 3 <> 0 Then szData = szData & Space(3 - Len(szData) Mod 3)
-
- For I = 1 To Len(szData) Step 3
- szOut = szOut & Chr(Asc(Mid(szData, I, 1)) \ 4 + 32)
- szOut = szOut & Chr((Asc(Mid(szData, I, 1)) Mod 4) * 16 + Asc(Mid(szData, I + 1, 1)) \ 16 + 32)
- szOut = szOut & Chr((Asc(Mid(szData, I + 1, 1)) Mod 16) * 4 + Asc(Mid(szData, I + 2, 1)) \ 64 + 32)
- szOut = szOut & Chr(Asc(Mid(szData, I + 2, 1)) Mod 64 + 32)
- Next I
-
- Encode = szOut
-
- End Function
-
- Function nMakeMsgWithFiles(szMsg As String, szFiles() As String, szOutputFile As String) As Integer
- '********************************************************************
- ' nMakeMsgWithFiles (by Carl Franklin)
- '
- ' This function creates an SMTP mail message
- ' with embedded uuencoded binary files. No header
- ' is created.
- '
- ' Parameters: szMsg Message text
- ' szFiles() List of filenames to be embedded
- ' szOutputFile Name of the file to be created
- '
- ' Returns: The function returns an error if
- ' one occurs.
- '********************************************************************
-
- Dim nMsgFile As Integer
- Dim nIndex As Integer
- Dim nErrCode As Integer
- Dim nUUEFile As Integer
- Dim szLine As String
-
- On Error GoTo nMakeMsgWithFiles_Error
-
- nMsgFile = FreeFile
-
- '-- Write the message portion to the file
- Open szOutputFile For Output As nMsgFile
- Print #nMsgFile, szMsg
- Print #nMsgFile, ""
- Close nMsgFile
-
- '-- Append all the encoded files to the master file.
- For nIndex = 1 To UBound(szFiles())
- nErrCode = UUEncode(szFiles(nIndex), szOutputFile, True)
- If nErrCode Then
- nMakeMsgWithFiles = Err
- Exit Function
- End If
- Next
-
- '-- Add the trailing period
- nMsgFile = FreeFile
- Open szOutputFile For Append As nMsgFile
- Print #nMsgFile, "."
- Close nMsgFile
-
- Exit Function
-
- nMakeMsgWithFiles_Error:
-
- nMakeMsgWithFiles = Err
- On Error Resume Next
- Close nMsgFile
- Exit Function
-
- End Function
-
- Function UUDecode(szFileIn As String, szFileOut As String) As Integer
-
- Dim nFileIn As Integer
- Dim nFileOut As Integer
- Dim szData As String
- Dim szOut As String
- Dim lBytesIn As Long
- Dim lFullLines As Long
-
- On Error GoTo ERR_UUDecode
-
- ' open the ascii input file
- nFileIn = FreeFile
- Open szFileIn For Input As nFileIn
-
- ' find the header in the input file
- While LCase(Left(Trim(szData), 6)) <> "begin "
- Line Input #nFileIn, szData
- Wend
-
- ' open the binary output file
- nFileOut = FreeFile
-
- ' if an output file wasn't given, take the name from the input file
- If szFileOut = "" Then
- szData = Trim(szData)
- szData = Trim(Mid(szData, InStr(szData, " ")))
- szFileOut = Trim(Mid(szData, InStr(szData, " ")))
- End If
-
- Open szFileOut For Binary As nFileOut
-
- Do While Not EOF(nFileIn)
-
- ' get a 45 bytes chunk, encode it and put it in the output file
- Line Input #nFileIn, szData
-
- If Trim$(LCase$(szData)) = "end" Then
- Exit Do
- ElseIf Trim$(szData) <> "" Then
- ' decode the input line and put it into the output file
- szOut = Left(Decode(Mid(szData, 2, Len(szData) - 1)), Asc(Left(szData, 1)) - 32)
- Put #nFileOut, , szOut
- End If
-
- Loop
-
- ' close the files
- Close nFileIn
- Close nFileOut
-
- ' if we got this far, then it must have worked!
- ' return of 0 means there were no errors
- UUDecode = 0
-
- Exit Function
-
- ERR_UUDecode:
- ' argghhh!, something went wrong, return the error code
- UUDecode = Err
-
- Close nFileIn
- Close nFileOut
-
- Exit Function
-
- End Function
-
- Function UUEncode(szFileIn As String, szFileOut As String, nAppend As Integer) As Integer
-
- Dim nFileIn As Integer
- Dim nFileOut As Integer
- Dim nIndex As Integer
- Dim szData As String
- Dim lBytesIn As Long
- Dim lFullLines As Long
-
- On Error GoTo ERR_UUEncode
-
- ' open the binary input file
- nFileIn = FreeFile
- Open szFileIn For Binary As nFileIn
- lBytesIn = LOF(nFileIn)
-
- ' open the ascii output file
- nFileOut = FreeFile
- If nAppend Then
- Open szFileOut For Append As nFileOut
- Else
- Open szFileOut For Output As nFileOut
- End If
-
- '-- Return just the filename portion of the outfile
- For nIndex = Len(szFileOut) - 1 To 1 Step -1
- If Mid$(szFileOut, nIndex, 1) = "\" Then
- szFileOut = Mid$(szFileOut, nIndex + 1)
- Exit For
- End If
- Next
-
- ' put the header in the output file
- Print #nFileOut, "begin 644 " & szFileIn
-
- ' determine how many full lines we get, 45 bytes gets
- ' expanded to 60 bytes
- lFullLines = lBytesIn \ 45
- szData = Space(45)
-
-
- While lFullLines > 0
-
- ' get a 45 bytes chunk, encode it and put it in the output file
- Get nFileIn, , szData
-
-
- Print #nFileOut, "M" & Encode(szData)
-
- ' another one "bytes" the dust
- lFullLines = lFullLines - 1
-
- Wend
-
- ' determine the leftover portion
- szData = Space(lBytesIn Mod 45)
-
- ' get the partial chunk of bytes that are left
- Get nFileIn, , szData
-
- ' put them in the output file
- Print #nFileOut, Chr(Len(szData) + 32) & Encode(szData)
-
- ' add on the file trailer
- Print #nFileOut, "end"
-
- ' close the files
- Close nFileIn
- Close nFileOut
-
- ' if we got this far, then it must have worked!
- ' return of 0 means there were no errors
- UUEncode = 0
-
- Exit Function
-
- ERR_UUEncode:
- ' argghhh!, something went wrong, return the error code
- UUEncode = Err
-
- Close nFileIn
- Close nFileOut
-
- Exit Function
-
- End Function
-